package com.mangofactory.aop.configuration
{
	import com.mangofactory.aop.SimpleCalculator;
	import com.mangofactory.aop.aspects.Aspect;
	import com.mangofactory.aop.aspects.LoggingAspect;
	import com.mangofactory.aop.interceptors.AspectInvokingInterceptor;
	
	import org.as3commons.reflect.Method;
	import org.as3commons.reflect.Type;
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertFalse;
	import org.flexunit.asserts.assertTrue;
	import org.hamcrest.assertThat;
	import org.hamcrest.collection.hasItem;
	import org.hamcrest.core.isA;
	

	public class MetatagProxyRegistryTests
	{
		/**
		 * function requiresProxy(clazz:Class):Boolean;
		function getAspectsForMethod(method:Method):Vector.<Aspect>;
		function getMethodsToProxy(type:Type):Vector.<Method>
		 * */
		[Test]
		public function testRequiresProxy():void
		{
			var policy:MetatagProxyPolicy = new MetatagProxyPolicy();
			// False before registering the metatag
			assertFalse(policy.requiresProxy(SimpleCalculator));

			policy.register("Log",new LoggingAspect());
			assertTrue(policy.requiresProxy(SimpleCalculator));
		}
		
		[Test]
		public function testGetAspectsForMethod():void
		{
			var policy:MetatagProxyPolicy = new MetatagProxyPolicy();
			policy.register("Log",new LoggingAspect());
			
			var type:Type = Type.forClass(SimpleCalculator);
			var aspects:Vector.<Aspect> = policy.getAspectsForMethod(type.getMethod("add"));
			assertEquals(1,aspects.length);
			assertThat(aspects[0],isA(LoggingAspect));
		}
		[Test]
		public function testGetMethodsToProxy():void
		{
			var policy:MetatagProxyPolicy = new MetatagProxyPolicy();
			policy.register("Log",new LoggingAspect());
			var type:Type = Type.forClass(SimpleCalculator);
			
			var methods:Vector.<Method> = policy.getMethodsToProxy(type);
			assertEquals(1,methods.length);
			assertEquals("add",methods[0].name);
		}
	}
}